package models

import (
	"strconv"
	"time"
)

type Iplist struct {
	*Model
	Url          string `json:"url"`
	Ip          string `json:"ip"`
	Port        string `json:"port"`
	Protocol    string `json:"protocol"`
	Service     string `json:"service"`
	Status		string `json:"status"`
	ResCode     string `json:"res_code"`
	ResResult   string `json:"res_result"`
	ResType     string `json:"res_type"`
	ResUrl      string `json:"res_url"`
	ResTitle    string `json:"res_title"`
	LoginUrl    string `json:"login_url"`
	Title       string `json:"title"`
	CreatedTime string `json:"created_time"`
	UpdatedTime string `json:"updated_time"`
}

func AddIplist(data map[string]interface{}) {
	nowTime := time.Now().Format("2006-01-02 15:04:05")
	IpList := Iplist{
		Url:          data["url"].(string),
		Ip:          data["ip"].(string),
		Port:        data["port"].(string),
		Protocol:    data["protocol"].(string),
		Service:     data["service"].(string),
		Status:		 data["status"].(string),
		ResCode:     data["res_code"].(string),
		ResResult:   data["res_result"].(string),
		ResType:     data["res_type"].(string),
		ResUrl:      data["res_url"].(string),
		ResTitle:    data["res_title"].(string),
		LoginUrl:    data["login_url"].(string),
		Title:       data["title"].(string),
		CreatedTime: nowTime,
		UpdatedTime: nowTime,
	}
	db.AutoMigrate(&IpList)
	db.Create(&IpList)
}

//判断数据库中是否存在ip和端口
func ExistIplist(ip, port string) (bool, int) {
	var iplist Iplist
	db.Select("id").Where("ip = ? and port = ? ", ip, port).First(&iplist)

	if iplist.ID > 0 {
		return true, iplist.ID
	}
	return false, iplist.ID
}

//查看资产

func GetIplist(pageNum int, pageSize int, maps interface{}) (iplist []Iplist) {
	dbTmp := db
	querys := maps.(map[string]interface{})

	if querys["url"] != nil {
		dbTmp = dbTmp.Where("url LIKE ?", "%"+querys["url"].(string)+"%")
	}
	if querys["ip"] != nil {
		dbTmp = dbTmp.Where("ip LIKE ?", "%"+querys["ip"].(string)+"%")
	}
	if querys["port"] != nil {
		dbTmp = dbTmp.Where("port LIKE ?", "%"+querys["port"].(string)+"%")
	}
	if querys["service"] != nil {
		dbTmp = dbTmp.Where("service LIKE ?", "%"+querys["service"].(string)+"%")
	}
	if querys["status"] != nil {
		dbTmp = dbTmp.Where("status LIKE ?", "%"+querys["status"].(string)+"%")
	}
	if querys["res_code"] != nil {
		dbTmp = dbTmp.Where("res_code LIKE ?", "%"+querys["res_code"].(string)+"%")
	}
	if querys["res_result"] != nil {
		dbTmp = dbTmp.Where("res_result LIKE ?", "%"+querys["res_result"].(string)+"%")
	}
	if querys["res_url"] != nil {
		dbTmp = dbTmp.Where("res_url LIKE ?", "%"+querys["res_url"].(string)+"%")
	}
	if querys["res_title"] != nil {
		dbTmp = dbTmp.Where("res_title LIKE ?", "%"+querys["res_title"].(string)+"%")
	}
	if querys["created_time"] != nil {
		dbTmp = dbTmp.Where("created_time LIKE ?", "%"+querys["created_time"].(string)+"%")
	}
	if querys["updated_time"] != nil {
		dbTmp = dbTmp.Where("updated_time LIKE ?", "%"+querys["updated_time"].(string)+"%")
	}

	dbTmp.Offset(pageNum).Limit(pageSize).Order("id  desc").Find(&iplist)

	return
}

func GetIplistTotal(maps interface{}) (count int) {
	dbTmp := db
	querys := maps.(map[string]interface{})

	if querys["url"] != nil {
		dbTmp = dbTmp.Where("url LIKE ?", "%"+querys["url"].(string)+"%")
	}
	if querys["ip"] != nil {
		dbTmp = dbTmp.Where("ip LIKE ?", "%"+querys["ip"].(string)+"%")
	}
	if querys["port"] != nil {
		dbTmp = dbTmp.Where("port LIKE ?", "%"+querys["port"].(string)+"%")
	}
	if querys["service"] != nil {
		dbTmp = dbTmp.Where("service LIKE ?", "%"+querys["service"].(string)+"%")
	}
	if querys["status"] != nil {
		dbTmp = dbTmp.Where("status LIKE ?", "%"+querys["status"].(string)+"%")
	}
	if querys["res_code"] != nil {
		dbTmp = dbTmp.Where("res_code LIKE ?", "%"+querys["res_code"].(string)+"%")
	}
	if querys["res_result"] != nil {
		dbTmp = dbTmp.Where("res_result LIKE ?", "%"+querys["res_result"].(string)+"%")
	}
	if querys["res_url"] != nil {
		dbTmp = dbTmp.Where("res_url LIKE ?", "%"+querys["res_url"].(string)+"%")
	}
	if querys["res_title"] != nil {
		dbTmp = dbTmp.Where("res_title LIKE ?", "%"+querys["res_title"].(string)+"%")
	}
	if querys["created_time"] != nil {
		dbTmp = dbTmp.Where("created_time LIKE ?", "%"+querys["created_time"].(string)+"%")
	}
	if querys["updated_time"] != nil {
		dbTmp = dbTmp.Where("updated_time LIKE ?", "%"+querys["updated_time"].(string)+"%")
	}

	dbTmp.Model(&Iplist{}).Count(&count)
	return
}

////判断数据库中ip是否存在，如果存在的话，返回URL
//func ExistUrl(ip string) (bool, int) {
//	var iplist Iplist
//	db.Select("id").Where("ip = ?",ip).First(&iplist)
//	if iplist.ID > 0 {
//		return true, iplist.ID
//	}
//	return false,iplist.ID
//}

//更新id时间
func EditIplist(id int, data interface{}) bool {
	db.Model(&Iplist{}).Where("id = ?", id).Updates(data)
	return true
}

//更新url+ip
func EditUrllist(id int,data map[string]interface{}) bool{
	db.Model(&Iplist{}).Where("id = ?", id).Updates(data)
	return true
}

func DeleteIplist(id int) (bool, int){
	db.Where("id = " + strconv.Itoa(id)).Delete(&Iplist{})
	return true, id
}
